﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TarreOA.TarreOAModels;
using TarreOA.TarreOABLL;
using TarreOA.TarreValidationCode;

public partial class DiscussArea_ThemeAppear : System.Web.UI.Page
{

    private PagedDataSource pds = null;
    private int dtid;
    private string curThemeUserLoginName;
    private User curLoginUser;

    protected void Page_Load(object sender, EventArgs e)
    {
        curLoginUser = (User) Session["user"];
        if(!Page.IsPostBack)
        {
            if(Session["user"] == null)
            {
                Response.Redirect("~/Login.aspx");
            }
            else if(Request.QueryString["dt_id"] != null)
            {
                dtid = int.Parse(Request.QueryString["dt_id"]);
                DiscussThemetbService dtService = new DiscussThemetbService();
                DiscussThemetb dtTb = dtService.FindById(dtid);
                if(dtTb != null)
                {
                    dtService.UpdateThemeHot(dtid);
                    this.curThemeUserLoginName = dtTb.DiscussThemeAuthor.LoginName;
                    this.LblAuthor.Text = dtTb.DiscussThemeAuthor.UserName;
                    //this.LblAuthor.Text = curLoginUser.UserName;
                    this.LblCreateTime.Text = dtTb.DiscussThemeCreateTime.ToString();
                    this.LblTitle.Text = dtTb.DiscussThemeTitle;
                    this.Label4.Text = dtTb.DiscussThemeTitle;
                    this.LblContent.Text = dtTb.DiscussThemeContent;
                    this.LblCurUser.Text = curLoginUser.UserName;
                    this.NewPagedDataSource();
                    this.DataList_DataBind();
                }
            }
            ViewState["pg_in"] = pds.CurrentPageIndex;
            ViewState["dtid"] = dtid;
            ViewState["curThemeUserLoginName"] = curThemeUserLoginName;
        }
    }

    private void NewPagedDataSource()
    {
        pds = new PagedDataSource();
        pds.AllowPaging = true;
        pds.PageSize = 10;
        this.DataList_DataTable();
    }

    private void DataList_DataTable()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("DtCommentsId", typeof(string));
        dt.Columns.Add("DtCommentsContent", typeof(string));
        dt.Columns.Add("DtCommentsCreateTime", typeof(string));
        dt.Columns.Add("DtCommentsAuthor", typeof(string));
        dt.Columns.Add("DtCommentsFloor", typeof(string));
        dt.Columns.Add("HylDelUrl", typeof(string));
        DiscussThemeCommentstbService dtcService = new DiscussThemeCommentstbService();
        List<DiscussThemeCommentstb> list = dtcService.FindALLByDtId(dtid);
        if(list != null && list.Count > 0)
        {
            int floor = 1;
            foreach(DiscussThemeCommentstb dtcTb in list)
            {
                DataRow dr = dt.NewRow();
                dr["DtCommentsId"] = dtcTb.DtCommentsId.ToString();
                dr["DtCommentsContent"] = dtcTb.DtCommentsContent;
                dr["DtCommentsCreateTime"] = dtcTb.DtCommentsCreateTime.ToString();
                dr["DtCommentsAuthor"] = dtcTb.DtCommentsAuthor.UserName;
                dr["DtCommentsFloor"] = "第<font color='red'><b>" + floor + "</b></font>楼";
                dr["HylDelUrl"] = "DelCom.aspx?TCID=" + dtcTb.DtCommentsId + "&TID=" + dtid;
                floor++;
                dt.Rows.Add(dr);
            }
            pds.DataSource = dt.DefaultView;
        }
        else
        {
            pds.DataSource = null;
        }
    }

    private void DataList_DataBind()
    {
        if(pds.DataSource == null)
        {
            this.DataList1.DataSource = null;
        }
        else
        {
            this.DataList1.DataSource = pds;
        }
        this.DataList1.DataBind();
    }
    protected void ImageButton3_Click(object sender, ImageClickEventArgs e)
    {
        if(ViewState["curThemeUserLoginName"] != null)
        {
            curThemeUserLoginName = (string) ViewState["curThemeUserLoginName"];
        }
        if(ViewState["dtid"] != null)
        {
            dtid = (int) ViewState["dtid"];
        }
        this.NewPagedDataSource();
        if(ViewState["pg_in"] != null)
        {
            pds.CurrentPageIndex = (int) ViewState["pg_in"];
        }
        pds.CurrentPageIndex--;
        this.DataList_DataBind();
        ViewState["pg_in"] = pds.CurrentPageIndex;
        ViewState["dtid"] = dtid;
        ViewState["cutThemeUserLoginName"] = curThemeUserLoginName;
    }
    protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
    {
        if(ViewState["curThemeUserLoginName"] != null)
        {
            curThemeUserLoginName = (string) ViewState["curThemeUserLoginName"];
        }
        if(ViewState["dtid"] != null)
        {
            dtid = (int) ViewState["dtid"];
        }
        this.NewPagedDataSource();
        if(ViewState["pg_in"] != null)
        {
            pds.CurrentPageIndex = (int) ViewState["pg_in"];
        }
        pds.CurrentPageIndex++;
        this.DataList_DataBind();
        ViewState["pg_in"] = pds.CurrentPageIndex;
        ViewState["dtid"] = dtid;
        ViewState["curThemeUserLoginName"] = curThemeUserLoginName;
    }
    protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            if(curLoginUser.LoginName.Equals(this.curThemeUserLoginName) || curLoginUser.PopedomGroup.PopedomGroupId == 1)
            {
                ((HyperLink) e.Item.FindControl("hylDel")).Visible = true;
                ((HyperLink) e.Item.FindControl("hylDel")).Attributes.Add("onclick", "return confirm('您确认要删除当前回复吗？')");
            }
        }
        if(e.Item.ItemType == ListItemType.Footer)
        {
            if(pds.IsFirstPage)
            {
                ((ImageButton) e.Item.FindControl("ImageButton3")).Enabled = false;
            }
            if(pds.IsLastPage)
            {
                ((ImageButton) e.Item.FindControl("ImageButton2")).Enabled = false;
            }
        }
    }
    protected void ImageButton4_Click(object sender, ImageClickEventArgs e)
    {
        if(ViewState["curThemeUserLoginName"] != null)
        {
            curThemeUserLoginName = (string) ViewState["curThemeUserLoginName"];
        }
        if(ViewState["dtid"] != null)
        {
            dtid = (int) ViewState["dtid"];
        }
        this.NewPagedDataSource();
        if(ViewState["pg_in"] != null)
        {
            pds.CurrentPageIndex = (int) ViewState["pg_in"];
        }
        if(!Code.Codes.Equals(Request.Form.Get(this.TxtValidationCode.ClientID)))
        {
            ScriptManager.RegisterClientScriptBlock(Page, GetType(), "", "alert('验证码输入错误！')", true);
        }
        else
        {
            User curLoginUser = (User) Session["user"];
            DiscussThemetbService dtService = new DiscussThemetbService();
            DiscussThemeCommentstbService dtcService = new DiscussThemeCommentstbService();
            string content = Request.Form.Get(this.TxtAppear.ClientID);
            if(dtcService.SaveDtCommentsInfo(content, DateTime.Now, curLoginUser.UserId, dtid) == 1)
            {
                ScriptManager.RegisterClientScriptBlock(Page, GetType(), "", "alert('回帖发表成功！')", true);
                dtService.UpdateThemeAppCount(dtid);
                this.DataList_DataTable();
                pds.CurrentPageIndex = pds.PageCount - 1;
                this.DataList_DataBind();
            }
            else
            {
                ScriptManager.RegisterClientScriptBlock(Page, GetType(), "", "alert('回帖发表失败，请重试！')", true);
            }
        }
        this.DataList_DataBind();
        ViewState["pg_in"] = pds.CurrentPageIndex;
        ViewState["dtid"] = dtid;
        ViewState["curThemeUserLoginName"] = curThemeUserLoginName;
    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("Index.aspx");
    }
}
